#ifndef BASICS_MY_DATE
#define BASICS_MY_DATE
//#include "stdafx.h"
class Basics_MyDate
{
	static const int inityear=1900;
	static const int initmonth=1;
	static const int initday=1;
	static int get_schaltjahrsum(int year)
	{
		if(year<inityear)return 0;
		else	return ((year-inityear)/4)*1461;
	}
	static int get_restjahrsum(int year)
	{
		int restjahr;
		int y;
		if(year<inityear)return 0;
		else{
			restjahr=((year-inityear)/4);
			restjahr=inityear+restjahr*4;
		}

		int sum=0;
		for (y=restjahr;y<=year;y++)
		{
			if (y%4<1){
				sum=sum+366;
			}
			else{
				sum=sum+365;
			}
		}
		return sum;
	}
	static int get_monthsum(int year, int mon)
	{
		int y;
		int sum=0;
		if(mon==0)return 0;
		for(y=1; y<=mon;y++)
		{
			sum=sum+get_mondur(year,y);
		}
		return sum;
	}
	static int get_year(double mydate,double &mydaterest)
	{
		int Schaltjahrzyklen;
		int year;
		int year_length;
		int stop=0;
		stop=0;
		Schaltjahrzyklen=(int)mydate/1461;
		year=inityear+Schaltjahrzyklen*4;
		mydaterest=mydate-Schaltjahrzyklen*1461;
		do{
			year_length=((year)%4>0)*365+(((year)%4)==0)*366;
			mydaterest=mydaterest-year_length;
			if(mydaterest>=0) 
				year++;
			else{
				mydaterest=mydaterest+year_length;
				stop=1;
			}
		}
		while (stop!=1);
		return year;

	}
	static int get_month(int year,double &mydaterest)
	{
		int monthnum=0;
		int month=0;
		int mondur;
		int stop=0;
		do{
			if (monthnum>11) monthnum=1;else monthnum++;
			mondur=get_mondur(year,monthnum);
			mydaterest=mydaterest-mondur;
			if(mydaterest>=0){
				month=monthnum;
			}
			else{
				mydaterest=mydaterest+mondur;
				stop=1;
			}
		}
		while(stop!=1);
		return month;
	}
	static double get_day(double mydaterest){
		return mydaterest;
		
	}
	static int check_GD_for_plausibility (int year,int mon,double day)
	{
		int i;
		if (year<inityear)err::err3("Basics_MyDate::check_GD_for_plausibility:year is to small");
		if (year>8000)err::err2("Basics_MyDate::check_GD_for_plausibility:year is to large");
		if ((mon<1)||(mon>12))err::err3("Basics_MyDate::check_GD_for_plausibility:month out of range");
		i=get_mondur(year,mon);
		if ((day<1)||(day>=i+1))err::err3("Basics_MyDate::check_GD_for_plausibility:day out of range");
		return 0;
		
	}
	static int check_Mydate_for_plausibility(double mydate)
	{
		if (mydate<0)err::err3("Basics_MyDate::check_Mydate_for_plausibility:mydate out of range");
		return 0;
	}
	public:
	static double Mydate_from_GD(int year,int mon,double day)
	{
		int schaltjahrsum;
		int restjahrsum;
		int monthsum;
		double daysum;
		Basics_MyDate::check_GD_for_plausibility(year,mon,day);
		schaltjahrsum=Basics_MyDate::get_schaltjahrsum(year-1);
		restjahrsum=Basics_MyDate::get_restjahrsum(year-1);
		monthsum=Basics_MyDate::get_monthsum(year,mon-1);
		daysum=day;
		return schaltjahrsum+restjahrsum+monthsum+daysum-1;
	}
	static int GD_from_Mydate (double mydate, int &year, int &mon, double &day)
	{
		double mydaterest=0;
		Basics_MyDate::check_Mydate_for_plausibility(mydate);
		year=Basics_MyDate::get_year(mydate,mydaterest);
		mon=Basics_MyDate::get_month(year,mydaterest)+1;
		day=Basics_MyDate::get_day(mydaterest)+1;
		return 0;

	}
	static int get_mondur(int year, int month)
		{
			int x;
			if ((year%4)==0)
			{
				switch (month)
				{
							case 1:x=31;
						   break;
							case 2:x=29;
						   break;
							case 3:x=31;
						   break;
						   case 4:x=30;
						   break;
						   case 5:x=31;
						   break;
						   case 6:x=30;
						   break;
						   case 7:x=31;
						   break;
						   case 8:x=31;
						   break;
						   case 9:x=30;
						   break;
						   case 10:x=31;
						   break;
						   case 11:x=30;
						   break;
						   case 12:x=31;
						   break;
				}
			}
			else
			{
				switch (month)
				{
							case 1:x=31;
						   break;
							case 2:x=28;
						   break;
							case 3:x=31;
						   break;
						   case 4:x=30;
						   break;
						   case 5:x=31;
						   break;
						   case 6:x=30;
						   break;
						   case 7:x=31;
						   break;
						   case 8:x=31;
						   break;
						   case 9:x=30;
						   break;
						   case 10:x=31;
						   break;
						   case 11:x=30;
						   break;
						   case 12:x=31;
						   break;
				}
			}
			return x;
		}
};
#endif